import { useUserStore } from '@/store/modules/user';

export const directivebt = (app) => {
  // 创建一个Vue指令
  app.directive('permission', {
    mounted(el, binding) {
      const userStore = useUserStore()
      // 检查权限
      if (!userStore.permissions.includes(binding.value)) {
        // 如果没有权限，移除按钮的点击事件和显示
        el.parentNode.removeChild(el)//直接把按钮从界面移除
        // el.style.display = 'none'; // 不显示按钮
      }
    }
  });
}
export const throttle = (app) => {
  app.directive('throttle', {
    mounted(el, binding) {
      let throttleTimeout = null;
      // 这是事件监听器，当事件触发时，节流函数会被调用
      el.addEventListener('click', () => {
        if (throttleTimeout) {
          clearTimeout(throttleTimeout)
          throttleTimeout=null
        }
        throttleTimeout = setTimeout(() => {
          binding.value.fn(binding.value.data)
        }, 500)
      });
    }
  }
  )
}
